package com.study.common.utils;

import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author nizhihao
 * @version 1.0.0
 * @date 2023/12/4 11:36
 */
public class ChuangLanSignUtils {

    public static final String SIGN = "sign";
    public static final Map<String, String> TEMPLATE_DATA = new HashMap<>();

    /**
     * 发送加签
     */
    public static String sendSign(String password, TreeMap<String, String> paramMap) {
        paramMap.remove(SIGN);
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
            if (StringUtils.isNotBlank(entry.getValue())) {
                sb.append(entry.getKey());
                sb.append(entry.getValue());
            }
        }
        return DigestUtils.md5DigestAsHex((sb + password).getBytes()).toLowerCase();
    }

    /**
     * 发送验签
     */
    public static boolean verifySendSign(String password, TreeMap<String, String> paramMap) {
        String sign = paramMap.remove(SIGN);
        String computed = sendSign(password, paramMap);
        return StringUtils.equals(sign, computed);
    }

}
